<template>
  <div class="trace-qr-infor-container">
    <div class="title-line1">溯源信息</div>
    <div class="main-box">
      <div class="title-line">
        <div class="title-line-left"></div>已扫描次数
        <div class="title-line-right">
          <div></div>
        </div>
      </div>

      <div class="scan-num-box">
        <div class="scan-num-box-left">{{ scanCountLeft }}</div>
        <div class="scan-num-box-right">{{ scanCountRight }}</div>
      </div>
      <productLayout
        v-if="traceCodeConfig.productFlag"
        id="productData"
        :item="traceInfo"
      ></productLayout>
      <proprietorLayout
        v-if="traceCodeConfig.proprietorFlag"
        id="proprietorData"
        :item="traceInfo"
      ></proprietorLayout>
      <individuationLayout
        v-if="traceCodeConfig.individuationFlag||traceCodeConfig.batchFlag"
        id="individuationInfo"
        :item="traceInfo"
      ></individuationLayout>
      <testReportInfoLayout
        v-if="traceCodeConfig.reportFlag"
        id="testReportInfo"
        :item="traceInfo"
      ></testReportInfoLayout>
      <environmentalDataLayout
        v-if="traceCodeConfig.environmentalFlag"
        id="environmentalData"
        :item="traceInfo"
      ></environmentalDataLayout>
    </div>
  </div>
</template>

<script>
import productLayout from "./components/product-info-layout/index";
import proprietorLayout from "./components/proprietor-info-layout/index";
import individuationLayout from "./components/individuation-info-layout/index";
import testReportInfoLayout from "./components/test-report-info-layout/index";
import environmentalDataLayout from "./components/environmental-data-info-layout/index";
import traceConfigApi from "@/api/trace-source-web/traceConfig.js";

export default {
  name: "traceability-information",
  components: {
    productLayout,
    proprietorLayout,
    individuationLayout,
    testReportInfoLayout,
    environmentalDataLayout,
  },
  data() {
    return {
      scanCountLeft: "",
      scanCountRight: "",
      traceInfo: {},
      traceCodeConfig: {},
    };
  },
  mounted() {
    this.queryTraceInfo();
  },
  methods: {
    queryTraceInfo() {
      let data = this.$route;
      console.log("queryTraceInfo", data.query);
      let traceConfigNo = data.query.traceConfigNo;
      let databaseName = data.query.databaseName;
      let organizationNo = data.query.organizationNo||'Test2';
      sessionStorage.setItem("databaseName", databaseName);
      sessionStorage.setItem("organizationNo", organizationNo);
      if (traceConfigNo) {
        traceConfigApi.traceInforQuery(traceConfigNo).then((res) => {
          console.log(res);
          let data = res.data;
          if (res.code === 1) {
            sessionStorage.setItem("farming", res.data.farming);
            sessionStorage.setItem('farmingTypeNo', res.data.farmingTypeNo)
            this.traceInfo = data;
            this.$set(this.traceCodeConfig, "productFlag", true);
            this.traceCodeConfig.proprietorFlag = true;
            this.traceCodeConfig.reportFlag = true;
            this.traceCodeConfig.environmentalFlag = true;
            if (data.individuationInfo) {
              this.traceCodeConfig.individuationFlag = true;
            } else {
              this.traceCodeConfig.batchFlag = true;
            }
            (this.scanCountRight = "1"),
              console.log(
                "traceInforQuery--traceCodeConfig",
                this.traceInfo,
                this.traceCodeConfig
              );
          } else {
            this.$message.warning(res.msg);
          }
        });
      } else {
        let tracingSourceCodeNo = data.query.tracingSourceCodeNo;
        let serialNumber = data.query.serialNumber;
        let params = {
          tracingSourceCodeNo,
          serialNumber,
        };
        traceConfigApi.traceQRInforQuery(params).then((res) => {
          console.log(res);
          let data = res.data;
          if (res.code === 1) {
            sessionStorage.setItem("farming", res.data.traceCodeConfig.farming);
            sessionStorage.setItem('farmingTypeNo', res.data.traceCodeConfig.farmingTypeNo)
            this.traceInfo = data.traceConfig;
            this.traceCodeConfig = data.traceCodeConfig;
            console.log("traceQRInforQuery--traceCodeConfig", this.traceInfo);
            this.rebuildScanTimes(data.tracingSourceCode.scanTimes);
          } else {
            this.$message.warning(res.msg);
          }
        });
      }
    },
    rebuildScanTimes(scanTimes) {
      if (scanTimes > 99) {
        scanTimes = 99;
      }
      if (scanTimes > 10) {
        let left = parseInt(scanTimes / 10);
        let right = scanTimes % 10;
        this.scanCountLeft = left;
        this.scanCountRight = right;
      } else {
        this.scanCountLeft = 0;
        this.scanCountRight = scanTimes;
      }

      console.log("rebuildScanTimes", this.scanCountLeft, this.scanCountRight);
    },
  },
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
.trace-qr-infor-container {
  display: flex;
  flex-direction: column;
  position: relative;
  width: 100%;
  height: 100%;
  overflow: auto;
  transition: 0.5s;
  background-color: #fff;
  box-sizing: border-box;
  .title-line1 {
    display: flex;
    justify-content: center;
    position: absolute;
    left: 0;
    top: 0;
    padding-top: 10px;
    font-weight: 500;
    font-family: Source Han Sans CN, Source Han Sans CN;
    font-size: 20px;
    color: #222c33;
    padding-top: 30px;
    height: 166px;
    width: 100%;
    background: linear-gradient(180deg, #acd5ff 0%, #63a4f0 100%);
    border-radius: 0px 0px 0px 0px;
  }
  .main-box {
    display: flex;
    flex-direction: column;
    position: absolute;
    left: 30px;
    top: 70px;
    width: calc(100% - 60px);
    height: calc(100% - 60px);
    background-color: #fff;
    box-sizing: border-box;
    .title-line {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      padding: 10px 0;
      font-weight: 500;
      font-size: 18px;
      font-family: PingFang SC, PingFang SC;
      color: #0086ff;
      text-align: center;
      font-style: normal;
      text-transform: none;
      box-sizing: border-box;
      .title-line-left {
        flex: 1;
        display: flex;
        align-items: center;
        height: 4px;
        box-sizing: border-box;
        background: url("~@/assets/traceability-qrcode/title-descript.png")
          no-repeat center;
        background-size: contain;
      }
      .title-line-right {
        flex: 1;
        display: flex;
        position: relative;
        z-index: 1;
        justify-content: flex-end;
        align-items: center;
        height: 100%;
        box-sizing: border-box;
        ::before {
          content: "";
          position: absolute;
          left: 0;
          width: 100%;
          height: 4px;
          background: url("~@/assets/traceability-qrcode/title-descript.png")
            no-repeat center;
          background-size: contain;
          transform: rotate(180deg);
        }

        .edit-box {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 49px;
          height: 32px;
          border-radius: 5px 5px 5px 5px;
          border: 1px solid #0480fb;
          cursor: pointer;
          &:hover {
            color: #1f74c7;
            background-color: #c7e3fe;
          }
        }
      }
    }
    .scan-num-box {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 40px;
      font-weight: 500;
      font-size: 34px;
      margin-bottom: 16px;
      font-family: Arial, Arial;
      color: #ffffff;
      text-align: center;
      font-style: normal;
      .scan-num-box-left,
      .scan-num-box-right {
        z-index: 2;
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 31px;
        height: 40px;
        background: linear-gradient(180deg, #acd5ff 0%, #63a4f0 100%);
        border-radius: 3px 3px 3px 3px;
        &::before {
          z-index: 1;
          content: "";
          position: absolute;
          left: 0;
          transform: translateY(-50%);
          width: 31px;
          height: 1px;
          background: #ffffff;
        }
      }
      .scan-num-box-left {
        margin-right: 10px;
      }
    }
  }
}
</style>
